<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>浏览器</title>

    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/yzl.css"/>

    <style>
        .browser-close {
            display: inline-block;
            position: absolute;
            width: 30px;
            height: 45px;
            top: 15px;
            left: 40px;
            background-size: 17px 17px;
            background-position: left;
            background-repeat:no-repeat;
        }

        .browser-close.big {
            background-image: url(../../image/big/btn_close_white.png);
        }

        .browser-close.small {
            background-image: url(../../image/small/btn_close_white.png);
        }

        .browser-title {
            margin: 0 60px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>

</head>
<body>
  <div class="header" id="app" v-cloak>
      <div id="back" class="back" style="width:30px;" :class="BSImg" onclick="back()" v-show="marketindex"></div>
      <div  v-myclick="backapp" style="float:right;margin-right:10px;margin-top:5px;font-size:14px" >返回牧场</div>
      <div id="browser-close" class="browser-close" :class="BSImg" v-show="changeTime >= 2" onclick="closeWin()"></div>
      <div  class="title browser-title">{{title}}</div>
  </div>
</body>
<script src="../../script/api.js"></script>
<script src="../../script/vue.js"></script>
<script src="../../script/app.js"></script>
<script src="../../script/common.js"></script>
<script src="../../script/url.js"></script>
<script>

    var webBrowser,
        url,
        loadUrl,
        orderId,
        vm;
    var state =1;

    apiready = function () {
        setBSImg();
        var back = document.getElementById('back');
        wx = api.require('wx');
        wxPay = api.require('wxPay');
        // var browserClose = document.getElementById('browser-close');
        // setTapmode([back, browserClose]);
        if(api.systemType=='ios'){
          webBrowser = api.require('webViewModule');
        }else{
          webBrowser = api.require('webViewModule');

        }
        MNActionButton = api.require('PIActionButton');
        // url = api.pageParam.url;

        url = yimahost+"buy.html?member_id="+getPrefs('member_id')+"&token="+getPrefs('token');
        url = api.pageParam.url;
        loadUrl = url;

        initApp();
        openWeb(url);
        addListeners();
        // webdoubleBack();

        api.parseTapmode();
    };

    function initApp() {
        vm = new Vue({
            el: '#app',
            data: {
                changeTime: 0,
                BSImg: {
                    big: Vue.BSImg == 'big',
                    small: Vue.BSImg == 'small'
                },
                title: '正在加载中...',
                orderId:orderId,
                marketindex:false
            },
            methods: {
              backapp:function(){
                api.sendEvent({
                  name: 'updateproperty',
              });
                api.closeWin();
              },
              pay_common:function(ret){
                if(ret.content.order_id!=undefined){
                  this.orderId = ret.content.order_id;
                }
                this.pay_wx(ret.content);
              },
              pay_wx: function (infos) {
                wxPay.payOrder({
                    apiKey: infos.apiKey,
                    orderId: infos.prepayid,
                    mchId: infos.partnerid,
                    nonceStr: infos.noncestr,
                    timeStamp: infos.timestamp,
                    package: infos.package,
                    sign: infos.sign
                }, function (ret, err) {

                    if (ret && ret.status) {
                        api.toast({
                            msg: '支付成功',
                            duration: 2000,
                            location: 'middle'
                        });

                        if(api.systemType=='ios'){
                          openWeb(loadUrl);
                        }else{
                          webBrowser.loadUrl({
                          url: loadUrl,
                          name:"market",
                          frameName:"market",
                      });
                        }

                    } else if (err.code == -2) {

                    } else {
                        alert('支付失败,错误码:' + err.code);
                    }
                });
            },

    },
        });
    }

    function back() {
      webBrowser.historyBack({
        frameName:'market',
        name:'market',
      },
          function (ret, err) {
            if (!ret.status) {
                closeWin();
                api.sendEvent({
                  name: 'updateproperty',
              });
            }
          }
      );
    }

    function openWeb(puturl) {
        var forbid_urlschemes = null;
        if (getPrefs('forbid_urlschemes')) {
            forbid_urlschemes = JSON.parse(getPrefs('forbid_urlschemes'));
        }
        webBrowser.openView({
            url: puturl,
            frameName:'market',
            name:'market',
            rect: {
                x: 0,
                y: 60,
                w: 'auto',
                h: api.winHeight-60
            },
            // forbid_urlschemes: forbid_urlschemes || ["tbopen", "tmall", "taobao", "openapp.jdmoble"],
        }, function (ret, err) {
          if(ret.state!=11){
            console.log(JSON.stringify(ret));
          }
          // webBrowser.loadScript({    script: 'api.alert({msg:"测试"})'});
            switch (ret.state) {            //加载状态，数字类型，取值范围：0-开始加载；1-加载进度发生变化；2-结束加载；3-title发生变化；4-url发生变化
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                if(ret.url.indexOf('buy.html')>-1||ret.url.indexOf('/sale.html')>-1){
                  vm.marketindex = false;
              }else{
                  vm.marketindex = true;
              }
                // var browser = api.require('webBrowser');
                    vm.changeTime++;
                    break;
                case 3:
                    vm.title = ret.title;
                    break;
                case 4:
                if(ret.url.indexOf(yimahost+'pay.html') > -1&&ret.url.indexOf('status=repay')==-1){
                  loadUrl =url;
                  var count = getUrlKey('count', ret.url);
                  var order_amount = getUrlKey('order_amount', ret.url);
                  var goods_id = getUrlKey('goods_id', ret.url);
                  var token = getUrlKey('token', ret.url);
                  var member_id = getUrlKey('member_id', ret.url);

                  var data = {
                      foreign_infos:goods_id+';'+count+';'+order_amount,
                      transfer_type: 3,
                      member_id:member_id,
                      order_amount: order_amount,
                      pay_type: 1,
                      order_type:0,
                      order_type:0,
                      good_id:goods_id,
                      count:count,
                  };
                  pinet.post(gtphost+'app/transaction/apppendbuy&token='+token, data, vm.pay_common);
                }
                if(ret.url.indexOf(yimahost+'pay.html') > -1&&ret.url.indexOf('status=repay')>-1){
                  loadUrl = url;
                  var order_id = vm.orderId;
                  var token = getUrlKey('token', ret.url);
                  var member_id = getUrlKey('member_id', ret.url);
                  var data = {
                      id:order_id,
                      member_id:member_id,
                  };
                  pinet.post(gtphost+'app/transaction/renewpay&token='+token, data, vm.pay_common);
                }
                if(ret.url.indexOf(yimahost+'topay.html') > -1){
                  loadUrl = yimahost+'mybuy.html';
                  var order_id = getUrlKey('order_id', ret.url);
                  var token = getUrlKey('token', ret.url);
                  var member_id = getUrlKey('member_id', ret.url);
                  var data = {
                      id:order_id,
                      member_id:member_id,
                  };
                  pinet.post(gtphost+'app/transaction/renewpay&token='+token, data, vm.pay_common);
                }
                    break;
                case 5:
                    break;
                case 6:
                    break;
                default:
                    break;
            }
        })

    }

    function addListeners(){
      api.addEventListener({
          name: 'market_back'
      }, function () {
          back();
      });
    }
</script>
</html>
